package org.lex.net.http;

import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.FileRequestEntity;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.log4j.Logger;

/**
 * @author Liu Er Xiao
 * @version 1.0, 2009-2-22
 */
public class RequestEntityFactory {
	public static final String DEFAULT_CONTENT_TYPE = "text/xml";
	public static final String DEFAULT_CHARSET = "UTF-8";

	private Logger log = Logger.getLogger(RequestEntityFactory.class);

	private String contentType = null;
	private String charset = null;

	private String fullContentType = null;

	// ====================================================
	// Constructors
	// ====================================================
	public RequestEntityFactory() {
		this(DEFAULT_CONTENT_TYPE);
	}

	public RequestEntityFactory(String contentType) {
		this(contentType, DEFAULT_CHARSET);
	}

	public RequestEntityFactory(String contentType, String charset) {
		this.contentType = contentType;
		this.charset = charset;
		this.fullContentType = getFullContentType();
	}

	// ====================================================
	// RequestEntity factory methods
	// ====================================================
	public RequestEntity createStringEntity(String requestBody) throws UnsupportedEncodingException {
		return new StringRequestEntity(requestBody, fullContentType, charset);
	}

	public RequestEntity createByteArrayEntity(byte[] requestData) {
		return new ByteArrayRequestEntity(requestData, fullContentType);
	}

	public RequestEntity createFileEntity(File file) {
		return new FileRequestEntity(file, fullContentType);
	}

	public RequestEntity createInputStreamEntity(InputStream in) {
		return new InputStreamRequestEntity(in, fullContentType);
	}

	// ====================================================
	// Attribute getters & setters
	// ====================================================
	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		if (null == contentType) {
			log.warn("ContentType is NULL, restore to default: " + DEFAULT_CONTENT_TYPE);
			contentType = DEFAULT_CONTENT_TYPE;
		}
		this.contentType = contentType;
		this.fullContentType = getFullContentType();
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		if (null == charset) {
			log.warn("charset is NULL, restore to default: " + DEFAULT_CHARSET);
			charset = DEFAULT_CHARSET;
		}
		this.charset = charset;
		this.fullContentType = getFullContentType();
	}

	private String getFullContentType() {
		return contentType + ";charset=" + charset;
	}
}
